package com.tiankong.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiankong.product.entity.Brand;
import com.tiankong.product.service.BrandService;
import net.youdi.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;


/**
 * <p>
 * 品牌 前端控制器
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @GetMapping("list")
    public Result list(@RequestParam(required = false,defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false,defaultValue = "1")Integer pageIndex,
                       @RequestParam(required = false)String key){
        Page<Brand> page = brandService.query().like("name",key).page(new Page<>(pageIndex, pageSize));
        return Result.ok().setPage(page.getTotal(),page.getRecords());
    }

    @PostMapping("save")
    public Result save(@RequestBody Brand brand){
        brandService.saveBrand(brand);
        return Result.ok(brand);
    }

    @GetMapping("info/{id}")
    public Result info(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        return Result.ok(brand);
    }
    @DeleteMapping("delete")
    public Result delete(@RequestBody Set<Long> ids){
        brandService.removeByIds(ids);
        return Result.ok();
    }
    @PutMapping("update/{id}/{status}")
    public Result updateStatus(@PathVariable("id")Long id, @PathVariable("status") Integer status){
        brandService.update().set("show_status",status).eq("brand_id",id).update();
        return Result.ok();
    }


}
